package com.lanou.dllo.shise.com.lanou.dllo.fragment;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.shise.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class TopStoryFragment extends Fragment{

	private ImageView adIv;//广告图片
	private TextView titleTv;//广告文字
	private String title;//标题
	private String imgurl;//图片地址
	private RequestQueue queue;//定义请求队列
	
	private static TopStoryFragment storyFragment;
	
	
	/**
	 * 单例模式
	 * 整个应用中保证只有一个实例对象
	 * 且必须是通过静态方法获取
	 * ***/
	public TopStoryFragment() {
		// TODO Auto-generated constructor stub
	}
	
	public static TopStoryFragment getInstance(String title, String imgurl) {
		
		TopStoryFragment storyFragment = new TopStoryFragment();
			
		
		//参数传递
		Bundle args = new Bundle();
		args.putString("title", title);
		args.putString("imgurl", imgurl);
		storyFragment.setArguments(args);
		
		return storyFragment;
		

	}
	
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		//初始化请求队列
		queue = Volley.newRequestQueue(activity);
		
	}
	
	
	
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//获取参数
		Bundle bundle = getArguments();
		if(bundle != null){
			title = bundle.getString("title");
			imgurl = bundle.getString("imgurl");
		}
		
	}
	
	
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		//加载布局文件
		View v = inflater.inflate(R.layout.fragment_top_story, null);
		adIv = (ImageView) v.findViewById(R.id.adIv);
		titleTv = (TextView) v.findViewById(R.id.titleTv);
		
		titleTv.setText(title);//设置标题
		getBitmap(imgurl, adIv);//拉取网络图片
		return v;
	}
	
	
	/***
	 * 通过Volley请求图片
	 * url 图片地址
	 * imageView 要显示的图片组件
	 * **/
	public void getBitmap(String url, final ImageView imageView) {
		
		ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
			
			@Override
			public void onResponse(Bitmap response) {
				// 将返回的bitmap图片对象设置到图片组件中
				imageView.setImageBitmap(response);
			}
		}, 720, 200, Config.ARGB_8888, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				error.printStackTrace();
				
			}
		}){
			public Map<String, String> getHeaders()
					throws AuthFailureError {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("Accept-Encoding", "");
				return map;
			}
			
		};
		queue.add(request);//将本次图片请求加入到请求队列中

	}
	
	
	
	
	
	
	
	
	
	
	
}
